//
// Created on 2024/4/30.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#ifndef DIANPING_CAPI_DEMO_FLOWITEMVIEWCOMPONENTINSTANCE_H
#define DIANPING_CAPI_DEMO_FLOWITEMVIEWCOMPONENTINSTANCE_H

#include "ComponentInstance.h"
#include "nodes/FlowItemNode.h"
#include "components/ComponentInstance.h"
#include "Model/FlowItemViewModel.h"
#include "common/ImagePrimitives.h"
#include "nodes/TextNode.h"
#include "nodes/ImageNode.h"


class FlowItemViewComponentInstance : public ComponentInstance, public FlowItemNodeDelegate {
private:
    FlowItemNode m_flowItemNode;
    std::shared_ptr<FlowItemViewModel> m_flowItemViewModel;
    TextNode m_textNode;
//     ImageNode m_imageNode;
    
public:
    FlowItemViewComponentInstance(Context context);
    ~FlowItemViewComponentInstance() {
        OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "FlowItemViewComponentInstance",
                     "~FlowItemViewComponentInstance");
    }
    void setModel(std::shared_ptr<ViewModel> model) override {
        m_flowItemViewModel = std::dynamic_pointer_cast<FlowItemViewModel>(model);
        build();
    }
    void build() override {
        if(m_flowItemViewModel == NULL){
            OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "FlowItemViewComponentInstance", "Empty View Model");
            return;
        }
        m_textNode.setTextContent(m_flowItemViewModel->getText());
        ImageSource src;
        src.uri = m_flowItemViewModel->getImage();
//         m_imageNode.setSource(src);
        m_flowItemNode.insertChild(m_textNode, 0);
//         m_flowItemNode.insertChild(m_imageNode, 0);
    }

    void onChildInserted(ComponentInstance::Shared const &childComponentInstance, std::size_t index) override;
    void onChildRemoved(ComponentInstance::Shared const &childComponentInstance) override;

    void onClick() override;
    FlowItemNode &getLocalRootArkUINode() override{
        return m_flowItemNode;
    }
};


#endif //DIANPING_CAPI_DEMO_FLOWITEMVIEWCOMPONENTINSTANCE_H
